package com.atomic;


import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author: csh
 * @Date: 2022/12/19 23:18
 * @Description:原子类学习
 */
public class AtomicStudy {
    public static void main(String[] args) {
//        AtomicInteger atomicInteger = new AtomicInteger();
//        for (int i = 0; i < 10; i++) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println(atomicInteger.getAndIncrement());
//                }
//            }).start();
//        }
//        AtomicLong atomicLong = new AtomicLong();
//        for (int i = 0; i < 10; i++) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println(atomicLong.getAndIncrement());
//                }
//            }).start();
//        }
//        AtomicBoolean atomicBoolean = new AtomicBoolean();
//       while (!atomicBoolean.get()){
//           new Thread(new Runnable() {
//               @Override
//               public void run() {
//                   try {
//                       Thread.sleep(100);
//                   } catch (InterruptedException e) {
//                       e.printStackTrace();
//                   }
//                   System.out.println(atomicBoolean.getAndSet(true));
//               }
//           }).start();
//       }
//        System.out.println("结束了！");

        AtomicReference<User> atomicReference = new AtomicReference<User>();
        User user = new User("1","用户1");
        User user2 = new User("2","用户2");
        atomicReference.set(user);
        boolean b = atomicReference.compareAndSet(user, user2);
        System.out.println(b);
        System.out.println(atomicReference.get().toString());


    }

    static class User{
        private String userName;
        private String userId;

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public User(String userName, String userId) {
            this.userName = userName;
            this.userId = userId;
        }

        @Override
        public String toString() {
            return "User{" +
                    "userName='" + userName + '\'' +
                    ", userId='" + userId + '\'' +
                    '}';
        }
    }
}
